﻿using IOA.MES.Business;
using IOA.MES.DataAccess.Enum;
using IOA.MES.DataAccess.VModel;
using IOA.MES.PdaApi.Models.Request.Outbound;
using IOA.MES.PdaApi.Models.Response.Outbound;
using System.Linq;
using System.Web.Http;

namespace IOA.MES.PdaApi.Controllers
{
    /// <summary>
    /// 出库管理
    /// </summary>
    public class OutboundController : ApiController
    {
        /// <summary>
        /// 出库请求
        /// </summary>
        [HttpPost]
        public ResultJson<OutboundResponse> Outbound([FromBody] OutboundRequest request)
        {
            new OutboundMgr().Outbound(new VOutboundPack
            {
                OutboundNo = request.OutboundNo,
                PackNo = request.PackNo,
                ModifyBy = BaseEnv.User.PkId
            });
            return new ResultJson<OutboundResponse>();
        }

        /// <summary>
        /// 查询出库记录
        /// </summary>
        [HttpPost]
        public ResultJson<QueryOutboundsResponse> QueryOutbounds([FromBody] QueryOutboundsRequest request)
        {
            var total = 0;
            var outbounds = new OutboundMgr().QueryOutbounds(request.PageIndex, request.PageSize, out total, BaseEnv.WarehouseCode, request.Finished ? new[] { OutboundStatus.Finished } : new[] { OutboundStatus.UnFinished }, request.Finished ? BaseEnv.User.PkId : 0);
            return new ResultJson<QueryOutboundsResponse>(new QueryOutboundsResponse
            {
                Outbounds = outbounds.Select(_ => new Outbound
                {
                    OutboundNo = _.OutboundNo,
                    Status = _.Status,
                    WarehouseCode = _.WarehouseCode,
                    WarehouseName = _.WarehouseName,
                    CabinetNo = _.CabinetNo,
                    OutboundBy = _.CNOutboundBy,
                    OutboundTime = _.OutboundTime,
                    Quantity = (int)_.Quantity,
                    OutboundedQuantity = (int)_.OutboundedQuantity
                }).ToList()
            });
        }

        /// <summary>
        /// 获取出库记录详情
        /// </summary>
        [HttpPost]
        public ResultJson<GetOutboundResponse> GetOutbound([FromBody] GetOutboundRequest request)
        {
            var outbound = new OutboundMgr().GetOutbound(request.OutboundNo);
            return new ResultJson<GetOutboundResponse>(new GetOutboundResponse
            {
                Outbound = outbound == null ? null : new Outbound
                {
                    OutboundNo = outbound.OutboundNo,
                    OutboundTime = outbound.OutboundTime,
                    CabinetNo = outbound.CabinetNo,
                    Status = outbound.Status,
                    OutboundBy = outbound.CNOutboundBy,
                    Quantity = (int)outbound.Quantity,
                    OutboundedQuantity = (int)outbound.OutboundedQuantity,
                    WarehouseCode = outbound.WarehouseCode,
                    WarehouseName = outbound.WarehouseName,
                    Packs = outbound.Products.Select(_ => new OutboundPack
                    {
                        AreaCode = _.AreaCode,
                        AreaName = _.AreaName,
                        Location = _.Location,
                        PackBatchNo = _.PackBatchNo,
                        PackNo = _.PackNo,
                        Outbounded = _.Outbounded
                    }).ToList()
                }
            });
        }
    }
}
